<?php
namespace App\Http\Controllers\Api\Device;

use Illuminate\Support\Facades\Log;


class BaseApi{
	/**
     * 接口get请求
     * @param  [type] $url [description]
     * @return [array]       [description]
     */
    public function CurlGet($url, $postdata){

    	$paramstr = $this->ArrayToStr($postdata);
        //echo $paramstr;exit;
    	$url 	= $url.'?'.$paramstr;
        $this->log(' get url', ['url' => $url]);

        $header = ['Accept: application/json'];
        $curl   = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_TIMEOUT, 1);
        // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
        
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        $data   = curl_exec($curl);   
        $error  = curl_error($curl);

        if (strpos($data, "请求错误") !== false || strpos($data, "HTTP Error") !== false){
            $this->log(" api request error ", ['data_error' =>$data]);
            return ['result' => -1, 'msg' => '接口异常'];
        }
        //dd($data, $error);
        if (!empty($error)) {
            $error   = json_decode($error,true);
        	if (is_array($error)) $this->log(" api error ", $error);
            curl_close($curl);
            return $error;
        }else{
            $data   = json_decode($data,true);
        	$this->log(" api success ",$data);
            curl_close($curl);
            return $data;
        }
        
    }
    
    /**
     * 接口post请求
     * @param  [type] $url       请求接口url
     * @param  [type] $postdata  请求数据
     * @return [array]           返回结果数组
     */
    public function CurlPost( $url, $postdata ) {

        $this->log(' post ', ['url' => $url]);
              //  dd($url,$postdata);

        //$postdata = json_decode($postdata, true);

        if (is_object($postdata)) $this->log(' param ',json_decode($postdata,true));
        if (is_array($postdata))  $this->log(' param ', $postdata);

        $header = ['Accept: application/json'];
        $curl   = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE );
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
        $data   = curl_exec($curl);
        $error  = curl_error($curl);
        //dd($data,$error);

        if (strpos($data, "请求错误") !== false){
            $this->log(" api request error ", ['data_error' =>$error]);
            return ['result' => -1, 'msg' => '请求错误'];
        }

        if ($error) {
            $error   = json_decode($error,true);
            $this->log(" api error ", $error);
            return $error;
        }else{
            $data   = json_decode($data,true);
            $this->log(" api success ",$data);
            return $data;
        }
        curl_close($curl);
    }

    /**
     * 写入系统日志
     * @return [type] [description]
     */
    public function log($type = "",$data, $prefix = 'yumai'){
        //var_dump($data);
        //if (is_array($data) || is_object($data)) $data = json_encode($data);
        Log::info($prefix.$type.':'.json_encode($data));
    }

    /**
     * 传递过来的数组转换为字符串
     * 例： $arra = ['key1' => 'name1', 'key2' => 'name2']
     * 结果 ：   key1=name1&key2=name2
     * 
     * @param [type] $arra [description]
     */
    public function ArrayToStr($arra)
    {
    	$paramstr = '';
    	if (!empty($arra))
        {
            foreach ($arra as $k => $v) {
                $paramstr.= $k.'='.$v.'&';
            }
            $paramstr = substr($paramstr, 0, -1);
        }
        return $paramstr;
    }
}
